var express = require('express');
var router = express.Router();
let {cartModel}=require('../../../model/model.js')

/* GET home page. */
router.get('/', function(req, res, next) {
  // res.render('index', { title: 'Express' });
  res.send('hello world');
});


router.post('/addcart',async(req,res)=>{
await cartModel.create(req.query)
console.log('====================================');
console.log(req.query);
console.log('====================================');
res.send('添加成功')
})


router.get('/getcart',async(req,res)=>{
  console.log(req.query);
  if(!req.query.userid){

    let data=await cartModel.find().populate('userid').populate('goodsid').populate('groupid')
    res.send({
      data:data,
      code:200,
      msg:'订单类别'
    })
  }else{
    let data=await cartModel.find().populate('userid').populate('goodsid').populate('groupid')
    .find({userid:JSON.parse(req.query.userid)})
    res.send({
      data:data,
      code:200,
      msg:'订单类别'
    })
  }
  
  
  
})

router.post('/subtract',async(req,res)=>{
  const {shopnum,id}=req.body.body
await cartModel.find({_id:id}).updateOne({shopnum:shopnum})
  res.send({
    code:200,
    msg:'修改成功'
  })
})


router.post('/updatechecked',async(req,res)=>{
  const {shop_id,user_id}=req.body.body

  await (cartModel.find({userid:user_id}).updateMany({checked:false}))
  let data=await cartModel.find({_id:shop_id}).updateMany({checked:true})
  let a=await cartModel.find({userid:user_id})
  
res.send({
  code:200,
  msg:'修改成功',
  data:data
})  
})

router.post('/addcart_shop',async(req,res)=>{
 
  const {shop_id,user_id}=req.body.body
  await cartModel.create({userid:user_id,goodsid:shop_id,groupid:'6725f25f0e757369cde96f8f',shopnum:1,checked:false,buy_time:new Date(),state:1})
  let data=await cartModel.find({userid:user_id})
  res.send({
    code:200,
    msg:'添加成功',
    data:data
  })
})

module.exports = router;
